package net.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式帮助类
 * 
 * @author 孙树林
 * 
 */
public class RegularUtils {

	/**
	 * 匹配表达式
	 * 
	 * @param reg
	 * @param value
	 * @return
	 */
	public static boolean expression(String reg, String value) {
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(value);
		return m.find();
	}
	
	/**
	 * 查找表达式
	 * @param reg
	 * @param value
	 * @return
	 */
	public static String search(String reg, String value) {
		String s = null;
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(value);
		if (m.find()) {
			s = m.group();
		}
		return s;
	}
	
	/**
	 * 查找表达式
	 * @param reg
	 * @param value
	 * @return
	 */
	public static List<String> searchList(String reg, String value) {
		List<String> s = new ArrayList<String>();
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(value);
		while (m.find()) {
			s.add(m.group());
		}
		return s;
	}

	public static void main(String[] args) {
		String s = "insert into t_v (id,name,x,y,z,e,ff) values (?,?,?,?,?,?)";
		//String s = "insert into t_v (id) values (?,?,?,?,?,?)";
		Pattern p = Pattern.compile("\\(([\\?]|([\\?],)+\\?)\\)");
		//String s = "update t_v set name=?,col3=?,col4=? where id=?";
		//String s = "update t_v set name=? where id=?";
		//Pattern p = Pattern.compile("\\w+=\\?");
		Matcher m = p.matcher(s);
		String t = null;
		while (m.find()) {
			t = m.group(); 
			System.out.println(t);
		}
		//System.out.println(t.substring(1, t.length() -1).split(","));
	}
}
